package rlyy.framework.netty.inithandler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author yuanqinglong
 * @since 2020/6/2 16:14
 */
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    private HttpServerHandler httpServerHandler;

    public HttpServerInitializer(HttpServerHandler httpServerHandler) {
        this.httpServerHandler = httpServerHandler;
    }


    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast("http-decoder",new HttpRequestDecoder());
        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(Integer.MAX_VALUE));
        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
        ch.pipeline().addLast("content-compressor",new HttpContentCompressor());// 默认压缩级别的处理器
        ch.pipeline().addLast("chunked-write", new ChunkedWriteHandler());//  大数据流的支持
        ch.pipeline().addLast("http-server", httpServerHandler);
    }
}
